#######################################################
# Example of a coupled DEVS model using two toaster DEVS models
# Author: moonho.hwang@gmail.com
#   Date: Oct.19.2011
#######################################################

from XSY.DEVS.CoupledDEVS import *
from XSY.DEVS.SimEngine import SimEngine
from Toaster import Toaster

def make_input_event(md, JSONarg=None):
   '''a callback function for injection'''
   ipn = raw_input("input ?push1 or ?push2>")
   if ipn in md.IP:
      ip = md.IP[ipn]
      return (md, PortValue(ip))
   else:
      return None # inject None into timer 

if __name__ == '__main__':
   print 'This example shows a coupled model "system" having two toaster "ts1" and "ts2" '
   print 'which can get started toasting by injection of "?push1" or "?push2", resectively'

   md1 = Toaster('ts1', toasting_time = 50)
   md2 = Toaster('ts2', toasting_time = 30)

   cm = CoupledDEVS("ts12", None)
   cm.addComponents(md1, md2)
   cm.addInputPorts("?push1", "?push2")
   cm.addOutputPorts("!pop1", "!pop2")

   cm.addCouple(cm.IP["?push1"], md1.IP["?push"])
   cm.addCouple(cm.IP["?push2"], md2.IP["?push"])
   cm.addCouple(md1.OP["!pop"], cm.OP["!pop1"])
   cm.addCouple(md2.OP["!pop"], cm.OP["!pop2"])

   engine = SimEngine(cm, make_input_event)
   engine.runInteractiveMenu()
